//1. load daily stock master file //

use "../dta/stock_daily.dta", clear
merge 1:1 code date using ../src/stock_daily_trading, keep(match) nogen
gen mkt="SH" if int(code/100000)==6
replace mkt="SZ" if mkt==""

//2. Merge the number of shareholders//
merge m:1 code y using ../src/holders_0809
drop if _m==2
drop _m

drop if y<2008
drop if m<10 & y==2008

merge m:1 code using ../src/holders_info
sort _merge
 drop if _merge==1
drop if _merge==2
drop _merge
count if m==.

replace no_holders=H1 if y==2009 & m>3 & m<=6
replace no_holders=H2 if y==2009 & m>6 & m<=9
replace no_holders=H3 if y==2009 & m>9
replace no_holders=H4 if y==2010 & m<=3
replace no_holders=H5 if y==2010 & m>3 & m<=6
replace no_holders=H6 if y==2010 & m>6 & m<=9
replace no_holders=H7 if y==2010 & m>9
replace no_holders=H8 if y==2011 & m<=3
replace no_holders=H9 if y==2011 & m>3 & m<=6
replace no_holders=H10 if y==2011 & m>6 & m<=9
replace no_holders=H11 if y==2011 & m>9
replace no_holders=H12 if y==2012 & m<=3
replace no_holders=H13 if y==2012 & m>3 & m<=6
replace no_holders=H14 if y==2012 & m>6 & m<=9
replace no_holders=H15 if y==2012 & m>9
replace no_holders=H16 if y==2013 & m<=3
replace no_holders=H17 if y==2013 & m>3 & m<=6
replace no_holders=H18 if y==2013 & m>6 & m<=9
replace no_holders=H19 if y==2013 & m>9
replace no_holders=H20 if y==2014 & m<=3
replace no_holders=H21 if y==2014 & m>3 & m<=6
replace no_holders=H22 if y==2014 & m>6 & m<=9
replace no_holders=H23 if y==2014 & m>9
replace no_holders=H24 if y==2015 & m<=3
replace no_holders=H25 if y==2015 & m>3 & m<=6
replace no_holders=H26 if y==2015 & m>6 & m<=9
replace no_holders=H27 if y==2015 & m>9
sort no_holders 
// 2016 no holders' information, null



//3. Merge the reform status and list date//
merge m:1 code using ../src/reform_list_info
drop if _merge==2
drop _merge
drop H1 H2 H3 H4 H5 H6 H7 H8 H9 H10 H11 H12 H13 H14 H15 H16 H17 H18 H19 H20 H21 H22 H23 H24 H25 H26 H27 H28

//4. Merge base index//
sort date
merge m:1 date using ../src/index_tot
sort _merge
drop if _merge==2

//5. For each stock, find corresponding benchmark index//
gen ccode=string( code)
replace ccode="0"+string(code) if length(ccode)==5
replace ccode="00"+string(code) if length(ccode)==4
replace ccode="000"+string(code) if length(ccode)==3
replace ccode="0000"+string(code) if length(ccode)==2
replace ccode="00000"+string(code) if length(ccode)==1

gen base_turn= turnover_sh if mkt=="SH"
replace base_turn= turnover_zx if mkt=="SZ" & substr(ccode,1,3)=="002"
replace base_turn= turnover_inv if mkt=="SZ" & substr(ccode,1,3)=="300"
replace base_turn= turnover_sz if mkt=="SZ" & base_turn==.

gen base_change= change_sh if mkt=="SH"
replace base_change= change_zx if mkt=="SZ" & substr(ccode,1,3)=="002"
replace base_change= change_inv if mkt=="SZ" & substr(ccode,1,3)=="300"
replace base_change= change_sz if mkt=="SZ" & base_change==.

gen base_clsprc= Clsprc_sh if mkt=="SH"
replace base_clsprc= Clsprc_zx if mkt=="SZ" & substr(ccode,1,3)=="002"
replace base_clsprc= Clsprc_inv if mkt=="SZ" & substr(ccode,1,3)=="300"
replace base_clsprc= Clsprc_sz if mkt=="SZ" & base_clsprc==.

gen base_hpr= Hpr_sh if mkt=="SH"
replace base_hpr= Hpr_zx if mkt=="SZ" & substr(ccode,1,3)=="002"
replace base_hpr= Hpr_inv if mkt=="SZ" & substr(ccode,1,3)=="300"
replace base_hpr= Hpr_sz if mkt=="SZ" & base_hpr==.

gen base_lpr= Lpr_sh if mkt=="SH"
replace base_lpr= Lpr_zx if mkt=="SZ" & substr(ccode,1,3)=="002"
replace base_lpr= Lpr_inv if mkt=="SZ" & substr(ccode,1,3)=="300"
replace base_lpr= Lpr_sz if mkt=="SZ" & base_lpr==.

//6. Calculate daily price change //
sort code date
by code: gen pclspr= price[_n-1]
gen change= (price-pclspr)/ pclspr*100

drop _m
sort code date
save ../dta/daily_tot.dta, replace
